package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.result.PageResult;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @version 1.0
 * @Author 郭腾高
 * @Date 2024/9/25 20:29
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /*
    * 添加购物车
    * */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        //根据用户id查询该用户购物车是否有此菜品或套餐，如果有口味信息 口味要相同
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        ShoppingCart shoppingCart1 = shoppingCartMapper.select(shoppingCart);
        //有，就数量加 1
        if(shoppingCart1!=null){
            shoppingCart1.setNumber(shoppingCart1.getNumber()+1);
            shoppingCartMapper.update(shoppingCart1);
        } else {
            //没有就加入相应菜品或套餐
            //判断本次添加的是菜品还是套餐
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId!=null){
              //本次添加菜品
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setNumber(1);
                shoppingCart.setCreateTime(LocalDateTime.now());

            }else {
                Setmeal setmeal = setmealMapper.getBySetmealId(shoppingCart.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setNumber(1);
                shoppingCart.setCreateTime(LocalDateTime.now());
            }
            //统一添加
            shoppingCartMapper.add(shoppingCart);
        }


    }
     /*
     * 查看购物车
     * */
    @Override
    public List<ShoppingCart> list() {
        Long currentId = BaseContext.getCurrentId();
        ShoppingCart build = ShoppingCart.builder()
                .userId(currentId)
                .build();
        List<ShoppingCart> list = shoppingCartMapper.list(build);
        return list;
    }
   /*
   * 清空购物车
   * */
    @Override
    public void deleteAll() {
        Long currentId = BaseContext.getCurrentId();
        ShoppingCart build = ShoppingCart.builder()
                .userId(currentId)
                .build();
        shoppingCartMapper.deleteAll(build);
    }
    /*
    * 删除一个购物车商品
    * */
    @Override
    public void deleteOne(ShoppingCartDTO shoppingCartDTO) {
       //查询获得菜品或套餐数量，大于1就数量减一，否则就删除
        ShoppingCart shoppingCart = new ShoppingCart();
        Long currentId = BaseContext.getCurrentId();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(currentId);
        ShoppingCart select = shoppingCartMapper.select(shoppingCart);
        Integer number = select.getNumber();
        if (number>1){
            select.setNumber(number-1);
            shoppingCartMapper.update(select);
        }else {
            shoppingCartMapper.deleteOne(shoppingCart);
        }

    }


}
